package com.hertz.appr.service;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.log4j.Logger;

import com.hertz.appr.pojo.db.Action;
import com.hertz.appr.pojo.db.Answer;
import com.hertz.appr.pojo.db.GuessResult;
import com.hertz.appr.pojo.db.PromoteResult;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

public class ResultTemplateService {

	private Logger log = Logger.getLogger(ResultTemplateService.class);
	private String templateDir;
	private String baseHtmlPath;
	private Configuration cfg ;
	private final String htmlEx = ".html";
	public ResultTemplateService()throws Exception{
		templateDir = com.hertz.appr.util.Configuration.getProperty("template.dir");
		baseHtmlPath = com.hertz.appr.util.Configuration.getProperty("html.path");
		cfg = new Configuration();
		cfg.setDirectoryForTemplateLoading(new File(templateDir));
		cfg.setEncoding(Locale.CHINA, "utf-8");
		//设置对象包装器
		cfg.setObjectWrapper(new DefaultObjectWrapper());
		
		//设置异常处理器
		cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
	}
	
	public boolean buildPromoteResult2Html(Action action, List<PromoteResult> list){
		log.debug(action);
		if(!action.getType().equals(Action.ACTION_TYPE_PROMOTE)){
			log.info("action.type is not guess ,type=" + action.getType());
			return false;
		}
		try {
			Template template = cfg.getTemplate("promoteResult.ftl");
			template.setEncoding("utf-8");
			//定义数据
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("action", action);
			data.put("list", list);
			//定义输出
			PrintWriter out 
				= new PrintWriter(new File(baseHtmlPath + action.getId() + htmlEx),"utf-8");
			template.process(data, out);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean buildGuessResult2Html(Action action, List<GuessResult> list){
		log.debug(action);
		if(!action.getType().equals(Action.ACTION_TYPE_GUESS)){
			log.info("action.type is not guess ,type=" + action.getType());
			return false;
		}
		try {
			Template template = cfg.getTemplate(com.hertz.appr.util.Configuration.getProperty("tempname.guess.result"));
			template.setEncoding("utf-8");
			//定义数据
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("action", action);
			data.put("list", list);
			//定义输出
			PrintWriter out 
				= new PrintWriter(new File(baseHtmlPath + action.getId() + htmlEx),"utf-8");
			template.process(data, out);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
	}
	
	public String getSendIdMsg(String idStr, String atUid){
		String msg = "";
		
		try {
			Template template = cfg.getTemplate("sendIdMsg.ftl");
			template.setEncoding("utf-8");
			//定义数据
			Map<String, String> data = new HashMap<String, String>();
			data.put("idStr", idStr);
			data.put("atUid", atUid);
			//定义输出
			StringWriter out = new StringWriter();
			
			template.process(data, out);
//			template.get
			msg = out.toString();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return msg;
		}
		return msg;
	}
	
	public String getTemplateContent(String templateName,Map<String, Object> data){
String msg = "";
		
		try {
			Template template = cfg.getTemplate(templateName);
			template.setEncoding("utf-8");
//			//定义数据
//			Map<String, String> data = new HashMap<String, String>();
//			data.put("idStr", idStr);
//			data.put("atUid", atUid);
			//定义输出
			StringWriter out = new StringWriter();
			if(data == null){
				data = new HashMap<String, Object>();
			}
			template.process(data, out);
//			template.get
			msg = out.toString();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return msg;
		}
		return msg;
	}

	public String getTemplateDir() {
		return templateDir;
	}

	public void setTemplateDir(String templateDir) {
		this.templateDir = templateDir;
	}

	public String getBaseHtmlPath() {
		return baseHtmlPath;
	}

	public void setBaseHtmlPath(String baseHtmlPath) {
		this.baseHtmlPath = baseHtmlPath;
	}
	
}
